home *** CD-ROM | disk | FTP | other *** search
/ Collection of Internet / Collection of Internet.iso / msdos / lynx / source / wattcp / src / pcping.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-25  |  1.8 KB  |  79 lines

  1. #include <mem.h>
  2. #include <copyright.h>
  3. #include <wattcp.h>
  4.  
  5. typedef struct icmp_echo {
  6.     byte    type;
  7.     byte    code;
  8.     word    checksum;
  9.     word    identifier;
  10.     word    sequence;
  11.     longword    index;
  12. };
  13.  
  14. typedef struct _pkt {
  15.     in_Header     in;
  16.     struct icmp_echo icmp;
  17.     in_Header    data;
  18. };
  19.  
  20.  
  21.  
  22. int _ping( longword host, longword countnum )
  23. {
  24.     eth_address dest;
  25.     struct _pkt *p;
  26.     in_Header *ip;
  27.     struct icmp_echo *icmp;
  28.     static word icmp_id = 0;
  29.  
  30.     if ((host & 0xff) == 0xff ) {
  31.     outs( "Cannot ping a network!\n\r");
  32.     return( -1 );
  33.     }
  34.     if ( ! _arp_resolve( host, &dest, 0 )) {
  35.     outs( "Cannot resolve host's hardware address\n\r");
  36.     return( -1 );
  37.     }
  38.  
  39.     if (debug_on) {
  40.     outs("\n\rDEBUG: destination hardware :");
  41.     outhexes( (byte *)&dest, 6 );
  42.     outs("\n\r");
  43.     }
  44.     p = (struct _pkt*)_eth_formatpacket( &dest, 8 );
  45.  
  46.     ip = &p->in;
  47.     memset( ip, 0, sizeof( in_Header ));
  48.     icmp = &p->icmp;
  49.  
  50.     icmp->type = 8;
  51.     icmp->code = 0;
  52.     icmp->index = countnum;
  53.     *(longword *)(&icmp->identifier) = set_timeout( 1 );
  54. /*
  55.     icmp->identifier = ++icmp_id;
  56.     icmp->sequence = icmp_id;
  57. */
  58.     /* finish the icmp checksum portion */
  59.     icmp->checksum = 0;
  60.     icmp->checksum = ~checksum( icmp, sizeof( struct icmp_echo));
  61.  
  62.     /* encapsulate into a nice ip packet */
  63.     ip->ver = 4;
  64.     ip->hdrlen = 5;
  65.     ip->length = intel16( sizeof( in_Header ) + sizeof( struct icmp_echo));
  66.     ip->tos = 0;
  67.     ip->identification = intel16( icmp_id ++);    /* not using ip id */
  68. //    ip->frag = 0;
  69.     ip->ttl = 250;
  70.     ip->proto = ICMP_PROTO;
  71.     ip->checksum = 0;
  72.     ip->source = intel( my_ip_addr );
  73.     ip->destination = intel( host );
  74.     ip->checksum = ~ checksum( ip, sizeof( in_Header ));
  75.  
  76.     return( _eth_send( intel16( ip->length )));
  77. }
  78.  
  79.